<template>
	<view class="">
		<!-- 搜索组件 -->
		<my-search @click="goSearch"></my-search>

		<view class="scroll-view-container">
			<!-- 左侧滑动盒子 -->
			<scroll-view class="left-scroll-view" scroll-y="true" :style="{height: wh+'px'}">
				<block v-for="(item, index) in cateList" :key="item.cat_id">
					<view :class="['scroll-view-item', active===index ? 'active' : '']" @click="onActive(index)">
						{{item.cat_name}}
					</view>
				</block>

			</scroll-view>
			<!-- 右侧滑动盒子 -->
			<scroll-view class="right-scroll-view" scroll-y="true" :style="{height: wh+'px'}" :scroll-top="scrollTop">
				<view class="scroll-lv2" v-for="item in cateLevel2" :key="item.cat_id">
					<view class="scroll-lv2-title">/ {{item.cat_name}} /</view>
					<view class="scroll-lv3-list">
						<view class="scroll-lv3-item" v-for="item1 in item.children" :key="item1.cat_id"
							@click="goGoodsList(item1)">
							<image :src="item1.cat_icon"></image>
							<text>{{item1.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import {
		getCateList
	} from '../../api/cate';
	import tabbarMix from '@/mixins/tabbar.js'
	export default {
		mixins: [tabbarMix],
		data() {
			return {
				wh: '',
				cateList: [],
				active: 0,
				cateLevel2: [],
				scrollTop: 0
			};
		},
		onLoad() {
			const res = uni.getWindowInfo()
			// console.log(res)
			this.wh = res.windowHeight - 50
			this.getCateList()
		},
		methods: {
			async getCateList() {
				const res = await getCateList()
				console.log(res)
				this.cateList = res.message
				this.cateLevel2 = res.message[0].children
			},
			onActive(i) {
				this.active = i
				this.cateLevel2 = this.cateList[i].children

				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			goGoodsList(item) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
				})
			},
			goSearch() {
				uni.navigateTo({
					url: '/subpkg/goods_search/goods_search'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;

			.scroll-view-item {
				background-color: #f7f7f7;
				line-height: 60px;
				text-align: center;
				font-size: 12px;

				&.active {
					background-color: #fff;
					position: relative;

					&::before {
						content: '';
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						top: 50%;
						left: 0;
						transform: translateY(-50%);
					}
				}
			}
		}

		.scroll-lv2-title {
			font-size: 12px;
			font-weight: bold;
			text-align: center;
			padding: 15px 0;
		}

		.scroll-lv3-list {
			display: flex;
			flex-wrap: wrap;

			.scroll-lv3-item {
				width: 33.33%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin-bottom: 10px;

				image {
					width: 60px;
					height: 60px;
				}

				text {
					font-size: 12px;
				}
			}
		}
	}
</style>
